package hot100;

public class searchMatrix2 {
    public boolean searchMatrix2(int[][] matrix, int target) {
        int i = matrix.length - 1;
        int j = 0;
        while(i >= 0 && j < matrix[0].length) {
            if(matrix[i][j] > target) {
                i--;
            }else if ( matrix[i][j] < target) {
                j++;
            }else {
                return true;
            }
        }
        return false;
    }
    public boolean searchMatrix(int[][] matrix, int target) {
        for(int i = 0;i < matrix.length;i++) {
            if(check(matrix[i],target)) {
                return true;
            }
        }
        return false;
    }

    boolean check(int[] nums,int target) {
        int left = 0;
        int right = nums.length - 1;
        int mid = 0;
        while(left <= right) {
            mid = left + (right-left) / 2;
            if(nums[mid] > target) {
                right = mid - 1;
            }else if(nums[mid] < target) {
                left = mid + 1;
            }else {
                return true;
            }
        }
        return false;
    }
}
